package trss.model.torrent;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;


import trss.bencode.BDictionary;
import trss.bencode.BElement;
import trss.bencode.BList;
import trss.bencode.BString;
import trss.bencode.Util;

@Entity
@Table(name = "metafile")
public class MetaFile {
    private static final String INFO = "info";
    private static final String ANNOUNCE_LIST = "announce-list";
    private static final String ENCODING = "encoding";
    private static final String CREATED_BY = "created by";
    private static final String COMMENT = "comment";
    private static final String CREATION_DATE = "creation date";
    private static final String ANNOUNCE = "announce";
    private Info info;
    private String announce = "";
    private List<AnnounceList> annonceList = new ArrayList<AnnounceList>();
    private Date creationDate = new Date();
    private String comment = "";
    private String createdBy = "";
    private String encoding = "";
    private Long id;

    public MetaFile() {
        super();
    }

    /**
     * 
     */
    public MetaFile(BDictionary dict) {
        if (dict == null) {
            throw new IllegalArgumentException();
        }
        announce = Util.getString(dict.get(ANNOUNCE));
        creationDate = new Date(Util.getLong(dict.get(CREATION_DATE)) * 1000);
        comment = Util.getString(dict.get(COMMENT));
        ;
        createdBy = Util.getString(dict.get(CREATED_BY));
        ;
        encoding = Util.getString(dict.get(ENCODING));
        ;
        makeAnnounceList((BList) (dict.get(ANNOUNCE_LIST)));
        info = new Info((BDictionary) dict.get(INFO));
    }

    /**
     * @param list
     *            the BList
     */
    private void makeAnnounceList(BList list) {
        if (list != null) {
            for (BElement bElement : list) {
                BList bElementList = (BList) bElement;
                AnnounceList l = new AnnounceList();
                for (BElement bElement2 : bElementList) {
                    String value = new String(((BString) bElement2).getValue());
                    l.add(value);
                }
                annonceList.add(l);
            }
        }
    }

    /**
     * @return the info
     */
    @OneToOne
    public Info getInfo() {
        return info;
    }

    /**
     * @param info
     *            the info to set
     */
    public void setInfo(Info info) {
        this.info = info;
    }

    /**
     * @return the announce
     */
    @Column
    //@SearchableProperty
    public String getAnnounce() {
        return announce;
    }

    /**
     * @param announce
     *            the announce to set
     */
    public void setAnnounce(String announce) {
        this.announce = announce;
    }

    /**
     * @return the annonceList
     */
//    @CollectionOfElements
    public List<AnnounceList> getAnnonceList() {
        return annonceList;
    }

    /**
     * @param annonceList
     *            the annonceList to set
     */
    public void setAnnonceList(List<AnnounceList> annonceList) {
        this.annonceList = annonceList;
    }

    /**
     * @return the creationDate
     */
    @Column
    public Date getCreationDate() {
        return creationDate;
    }

    /**
     * @param creationDate
     *            the creationDate to set
     */
    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    /**
     * @return the comment
     */
    @Column
//    @SearchableProperty
    public String getComment() {
        return comment;
    }

    /**
     * @param comment
     *            the comment to set
     */
    public void setComment(String comment) {
        this.comment = comment;
    }

    /**
     * @return the createdBy
     */
    @Column
//    @SearchableProperty
    public String getCreatedBy() {
        return createdBy;
    }

    /**
     * @param createdBy
     *            the createdBy to set
     */
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    /**
     * @return the encoding
     */
    @Column
    public String getEncoding() {
        return encoding;
    }

    /**
     * @param encoding
     *            the encoding to set
     */
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    /**
     * @return the id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
//    @SearchableId
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * ${@inheritDoc}
     */
    @Override
    public String toString() {
        return "MetaFile [info=" + info + ", announce=" + announce
                + ", annonceList=" + annonceList + ", creationDate="
                + creationDate + ", comment=" + comment + ", createdBy="
                + createdBy + ", encoding=" + encoding + "]";
    }

    /**
     * ${@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((annonceList == null) ? 0 : annonceList.hashCode());
        result = prime * result
                + ((announce == null) ? 0 : announce.hashCode());
        result = prime * result + ((comment == null) ? 0 : comment.hashCode());
        result = prime * result
                + ((createdBy == null) ? 0 : createdBy.hashCode());
        result = prime * result
                + ((creationDate == null) ? 0 : creationDate.hashCode());
        result = prime * result
                + ((encoding == null) ? 0 : encoding.hashCode());
        result = prime * result + ((info == null) ? 0 : info.hashCode());
        return result;
    }

    /**
     * ${@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        MetaFile other = (MetaFile) obj;
        if (annonceList == null) {
            if (other.annonceList != null) {
                return false;
            }
        } else if (!annonceList.equals(other.annonceList)) {
            return false;
        }
        if (announce == null) {
            if (other.announce != null) {
                return false;
            }
        } else if (!announce.equals(other.announce)) {
            return false;
        }
        if (comment == null) {
            if (other.comment != null) {
                return false;
            }
        } else if (!comment.equals(other.comment)) {
            return false;
        }
        if (createdBy == null) {
            if (other.createdBy != null) {
                return false;
            }
        } else if (!createdBy.equals(other.createdBy)) {
            return false;
        }
        if (creationDate == null) {
            if (other.creationDate != null) {
                return false;
            }
        } else if (!creationDate.equals(other.creationDate)) {
            return false;
        }
        if (encoding == null) {
            if (other.encoding != null) {
                return false;
            }
        } else if (!encoding.equals(other.encoding)) {
            return false;
        }
        if (info == null) {
            if (other.info != null) {
                return false;
            }
        } else if (!info.equals(other.info)) {
            return false;
        }
        return true;
    }

}
